import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

class EnterQueuePage extends StatefulWidget {
  const EnterQueuePage({super.key});

  @override
  State<StatefulWidget> createState() => _EnterQueuePageState();
}

class _EnterQueuePageState extends State<EnterQueuePage> {
  final textFieldController = TextEditingController();

  void confirmJoin() {
    showDialog(
        context: context,
        builder: (context) {
          return AlertDialog(
            title: Text(
                'Are you sure to join queue: ${textFieldController.text}?'),
            content: Text('There are 2 person in queue.'),
            actions: [
              TextButton(
                  onPressed: () =>
                  {
                    Navigator.of(context, rootNavigator: true).pop()
                  },
                  child: const Text('Cancel')),
              TextButton(
                  onPressed: () {
                    Navigator.of(context, rootNavigator: true).pop();
                    Navigator.pushNamed(context, '/queuer');
                  },
                  child: const Text('Join'))
            ],
          );
        });
  }

  @override
  void initState() {
    super.initState();
  }

  @override
  void dispose() {
    textFieldController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Theme
            .of(context)
            .colorScheme
            .inversePrimary,
        title: Text('Enter Queue'),
      ),
      body: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              const Text('Input your queue list number:'),
              Padding(padding: EdgeInsets.all(8)),
              Padding(
                padding: const EdgeInsets.all(8.0),
                child: SizedBox(
                  width: 200,
                  child: TextField(
                    controller: textFieldController,
                    keyboardType: TextInputType.number,
                    inputFormatters: [
                      LengthLimitingTextInputFormatter(4),
                      FilteringTextInputFormatter.allow(RegExp(r'[0-9]')),
                    ],
                    decoration: InputDecoration(
                      border: OutlineInputBorder(),
                      labelText: 'Queue List Number',
                    ),
                  ),
                ),
              ),
              Padding(padding: EdgeInsets.all(8)),
              ElevatedButton.icon(
                onPressed: () => {confirmJoin()},
                label: const Text('Join Queue'),
                icon: Icon(Icons.arrow_right_alt),
                iconAlignment: IconAlignment.end,
              ),
            ],
          )),
    );
  }
}
